<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class web extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('html','url','cookie'));
		$this->load->library(array('common'));
		$this->load->model('pub_model');
	}
	
	public function index()
	{
		//$this->output->enable_profiler(TRUE);
		$data['doctype'] = doctype('xhtml1-trans');
		$data['meta_charset'] = meta('Content-type','text/html;charset=utf-8','equiv');
		$data['linkiscroll'] = link_tag('css/iscroll.css');
		$data['link'] = link_tag('css/web.css');
		$data['jquery'] = '<script src="'.base_url().'script/jquery.js"></script>';
		$data['jsiscroll'] = '<script src="'.base_url().'script/iscroll4.2.min.js"></script>';
		$data['webcommon'] = '<script src="'.base_url().'script/web.common.js?123413"></script>';
		
		$data['skey']=SinaAppKey;
		$data['tkey']=TencentAppKey;
		
		$re='';
		
		if(!empty($_GET['code']) && $this->input->cookie('dis')==''){
			$ad=$_GET['code'];
		
			$url='https://api.weibo.com/oauth2/access_token';
			$key=SinaAppKey;
			$secret=SinaAppSecret;
			$type='authorization_code';
			$code=$_GET['code'];
			$uri=base_url().'index.php?c=web&m=index';
		
			$re=$this->getTk($url,$key,$secret,$type,$code,$uri);
		
			if($re!=''){
				$obj=json_decode($re);
			
				$cookie = array(
				    'name'   => 'dis',
				    'value'  => $obj->{'uid'}.'_'.$obj->{'access_token'},
				    'expire' => $obj->{'expires_in'}
				);

				$this->input->set_cookie($cookie);
			}
		}

		$this->load->view('web/index',$data);
	}
	
	public function listcontent(){	
		//$this->output->enable_profiler(TRUE);
		$result=$this->pub_model->listcontent($this->input->post('page',TRUE));
		$str='<ul>';
		$d=1;
		
		foreach($result->result() as $row){
			if($d%2>0){
				$str.='<li class="pic"><img src="/images/web/pic'.$row->pic.'-1.png" width="100" height="100" border="0" /></li>';
				$str.='<li class="leftcont"><p>'.$row->content.'</p></li>';
				$str.='<li class="point"></li>';				
			}else{
				$str.='<li class="rightcont"><p>'.$row->content.'</p></li>';
				$str.='<li class="pic"><img src="/images/web/pic'.$row->pic.'-1.png" width="100" height="100" border="0" /></li>';
				$str.='</ul><ul>';				
			}
			
			$d++;
		}
		
		$str.='</ul><div style="clear:both;"></div>';
		
		echo $str;
	}
	
	public function countnum(){	
		$result=$this->pub_model->countnum();
		$str='';
		
		foreach($result->result() as $row){
			$str=$row->countnum;
		}
		
		echo $str;
	}
	
	public function save(){
		$url='https://api.weibo.com/2/statuses/update.json';
		$access_token=urldecode($_POST['access_token']);
		$openid=urldecode($_POST['openid']);
		$status=urldecode($_POST['status']);
		$content=urldecode($_POST['content']);
		$pic=urldecode($_POST['pic']);

		$re=$this->setWB($url,$access_token,'#怪物神枪手Luckyline#'.$content.'http://lucky.morningtec.cn');
		
		echo $this->pub_model->save($openid,$status,$content,$pic);
	}
	
	function getTk($url,$key,$secret,$type,$code,$uri){
		$pdata = array ('client_id' => $key,
			'client_secret' => $secret,
			'grant_type' => $type,
			'code' => $code,
			'redirect_uri' => $uri
		);
		$pdata = http_build_query($pdata);

		$params = array('http' => array(
			'method' => 'POST',
			'content' => $pdata
		));

		$ctx = stream_context_create($params);
		$fp = @fopen($url, 'rb', false, $ctx);
		$response = @stream_get_contents($fp);
		
		return $response;
	}
	
	function setWB($url,$access_token,$status){
		$pdata = array ('access_token' => $access_token,
			'status' => $status
		);
		$pdata = http_build_query($pdata);

		$params = array('http' => array(
			'method' => 'POST',
			'content' => $pdata
		));

		$ctx = stream_context_create($params);
		$fp = @fopen($url, 'rb', false, $ctx);
		$response = @stream_get_contents($fp);
		
		return $response;
	}
}



/* End of file web.php */
/* Location: ./application/controllers/web.php */